package com.android.woodpecker;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseScore {
	public static final String ROW_ID = "_id";
	//public static final String ROW_NAME = "name";
	public static final String ROW_SCORE = "score";

	private static final String DATABASE_NAME = "scoredb";
	private static final String DB_TABLE_TRIBLE_SUM = "triblesum";
	private static final int DATABASE_VERSION = 1;

	private final Context ourContext;
	private DatabaseHelper ourHelper;
	private SQLiteDatabase ourDatabase;

	public class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE " + DB_TABLE_TRIBLE_SUM + " (" + ROW_ID
					+ " INTEGER PRIMARY KEY AUTOINCREMENT, " 
					//+ ROW_NAME + " TEXT NOT NULL, " 
					+ ROW_SCORE + " TEXT NOT NULL" + ")");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE_TRIBLE_SUM);
			onCreate(db);
		}
	}

	public DatabaseScore(Context c) {
		ourContext = c;
	}

	public DatabaseScore Open() throws SQLException {
		ourHelper = new DatabaseHelper(ourContext);
		ourDatabase = ourHelper.getWritableDatabase();
		return this;
	}

	public void Close() throws SQLException {
		ourHelper.close();
	}

	public String getData() {
		String[] columns = { ROW_ID, ROW_SCORE };
		Cursor c = ourDatabase.query(DB_TABLE_TRIBLE_SUM, columns, null, null,
				null, null, ROW_SCORE);
		String result = "";

		int iRow = c.getColumnIndex(ROW_ID);
		//int iRowName = c.getColumnIndex(ROW_NAME);
		int iRowScore = c.getColumnIndex(ROW_SCORE);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			result += c.getString(iRow) + "\t" 
				//	+ c.getString(iRowName) + "\t "
					+ c.getString(iRowScore) + "\n";
		}
		return result;
	}

	public Long createEntry(int score) {
		ContentValues cv = new ContentValues();
		cv.put("Score", score);
		return ourDatabase.insert(DB_TABLE_TRIBLE_SUM, null, cv);
	}

	public ArrayList<ArrayList<Object>> getAllRowsAsArrays() {
		// create an ArrayList that will hold all of the data collected from
		// the database.
		ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
 
		// this is a database call that creates a "cursor" object.
		// the cursor object store the information collected from the
		// database and is used to iterate through the data.
		Cursor cursor;
 
		try
		{
			// ask the database object to create the cursor.
			cursor = ourDatabase.query(
					DB_TABLE_TRIBLE_SUM,
					new String[]{ROW_ID, ROW_SCORE},
					null, null, null, null, null
			);
 
			// move the cursor's pointer to position zero.
			cursor.moveToFirst();
 
			// if there is data after the current cursor position, add it
			// to the ArrayList.
			if (!cursor.isAfterLast())
			{
				do
				{
					ArrayList<Object> dataList = new ArrayList<Object>();
 
					dataList.add(cursor.getLong(0));
					dataList.add("Player ");
					dataList.add(cursor.getString(1));
 
					dataArrays.add(dataList);
				}
				// move the cursor's pointer up one position.
				while (cursor.moveToNext());
			}
		}
		catch (SQLException e)
		{
			Log.e("DB Error", e.toString());
			e.printStackTrace();
		}
 
		// return the ArrayList that holds the data collected from
		// the database.
		return dataArrays;
	}
}
